package com.neeraj.SharingArtifacts;

import java.io.File;
import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class AudioTab extends ListActivity {
	private static final String DEBUG_TAG = "AUDIO TAB";
	private static AudioTab instance;
	private ArrayList<String> child;
	private ListView listviewAudio;
	DatabaseHelper dbHelper;
	ArrayList<String> checkedAudioItems;
	ArrayAdapter<String> fileList;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_layout);
        instance = this;
        listviewAudio = (ListView)findViewById(android.R.id.list);
        dbHelper=new DatabaseHelper(this);
		dbHelper.insertTypeData();
        showAllAudioFiles();
    }
    
    public static AudioTab getInstance() {
    	return instance;
    }
    
    void showAllAudioFiles(){
    	File dir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_DCIM), "SharingArtifacts/audio");
    	String[] files = dir.list();

    	child = new ArrayList<String>();
    	checkedAudioItems = new ArrayList<String>();
    	if (files == null) {
  	    // Either dir does not exist or is not a directory
    	} else {
    		for (int i=0; i<files.length; i++) {
  	        // Get filename of file or directory    	    	
    			String children = files[i];
    			child.add(children);    	        	    	    	        
    		}
    	}
    	Log.d(DEBUG_TAG, child.toString());
    	
    	fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, child);
    	listviewAudio.setAdapter(fileList);
    	ListView list = getListView();
    	list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    	
    	listviewAudio.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String item = (String) listviewAudio.getAdapter().getItem(arg2);
				CheckedTextView listItem = (CheckedTextView)arg1;
				if(!listItem.isChecked()){
					System.out.println(item + ":Status=" + dbHelper.isUploaded(item));
					if(!dbHelper.isUploaded(item)){
						Log.d(DEBUG_TAG, "Checked!!");
						checkedAudioItems.add(item);
					}
				}
				else
					Log.d(DEBUG_TAG, "Not Checked!!");
				Log.d(DEBUG_TAG, item);				
			}
		});
    }

    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	super.onBackPressed();
    	Toast.makeText(this, checkedAudioItems.toString(), Toast.LENGTH_LONG).show();
    }
    
}